package com.sjtuy.galtxtreader.bookfile;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.Vector;

public class PunctuationsBasedDialogSplitter implements DialogSplitter {
    char[] RIGHT_PUNCS={'》','。','！','？','”','：','；'};
    private int maxLength;
    private int likelyLength;
    private SimpleDialogSplitter simpleDialogSplitter;
    public PunctuationsBasedDialogSplitter(int likelyLength,int maxLength){
        this.likelyLength=likelyLength;
        this.maxLength=maxLength;
        simpleDialogSplitter=new SimpleDialogSplitter(likelyLength,maxLength);
    }
    @Override
    public String split(String paragraph) {
        StringBuffer sb=new StringBuffer();
        char[] paragraphCharSet= paragraph.toCharArray();
        Vector<Integer> splitIndexList = new Vector<>();

        for(int i=1;i<paragraphCharSet.length-1;++i){
            if(isRightPuncs(paragraphCharSet[i])&&!isRightPuncs(paragraphCharSet[i+1]))
                splitIndexList.add(i+1);
        }
        int currentLength=0;
        if(splitIndexList.isEmpty()){
            return simpleDialogSplitter.split(paragraph);
        }
        for(int i=0;i<splitIndexList.size();++i){
            int thisIndex=splitIndexList.get(i);
            int nextIndex=(i==(splitIndexList.size()-1))?paragraph.length():splitIndexList.get(i+1);
            if(thisIndex-currentLength>likelyLength&&thisIndex-currentLength<=maxLength){
                sb.append(paragraph.substring(currentLength, thisIndex)).append("\n");
                currentLength=thisIndex;
                continue;
            }
            if(thisIndex-currentLength<=likelyLength){
                if(nextIndex-currentLength<likelyLength){
                    if(i+1==splitIndexList.size()){
                        sb.append(paragraph.substring(currentLength,nextIndex)).append("\n");
                        break;
                    }
                    continue;
                }
                if(nextIndex-currentLength<maxLength){
                    sb.append(paragraph.substring(currentLength,nextIndex)).append("\n");
                    currentLength=nextIndex;
                    ++i;
                    continue;
                }
                sb.append(paragraph.substring(currentLength, thisIndex)).append("\n");
                currentLength=thisIndex;
                continue;
            }
            if(thisIndex-currentLength>maxLength){
                sb.append(simpleDialogSplitter.split(paragraph.substring(currentLength,thisIndex)));
                currentLength=thisIndex;
                i--;
                continue;
            }
        }
        if(currentLength<paragraph.length()-1){
            sb.append(simpleDialogSplitter.split(paragraph.substring(currentLength)));
        }
        return sb.toString();
    }
    private boolean isRightPuncs(char c){
        for(char p:RIGHT_PUNCS){
            if(c==p)
                return true;
        }
        return false;
    }
}
